<template style="overflow-y: scroll; overflow-x: hidden; height: 100%">
  <el-drawer
    title="员工管理"
    size="37%"
    :visible.sync="dialog"
    custom-class="demo-drawer"
  >
    <div class="demo-drawer__content">
      <el-row style="margin-left: 30px">
        <el-col :span="24">
          <el-tooltip effect="dark" content="添加员工" placement="top">
            <el-button class="filter-item" size="mini" round circle icon="el-icon-plus" @click="openCreateDialog"
            ></el-button>
          </el-tooltip>
        </el-col>
        <el-col :span="24">
          <el-table
            :data="userList"
            style="width: 100%"
          >
            <el-table-column
              prop="userName"
              label="名称"
            >
            </el-table-column>
            <el-table-column
              width="100px"
              label="操作"
            >
              <template slot-scope="{row}">
                <el-button v-if="$store.state.user.userId != row.userId" type="danger" @click="removeUser(row)"
                           size="mini"
                >移出
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-dialog
        title="添加用户"
        :visible.sync="createDialog"
        append-to-body
        @close="closeDialog"
        width="30%"
      >
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="用户编码" prop="userCode">
            <el-input v-model="form.userCode"></el-input>
          </el-form-item>
          <el-form-item label="用户名称" prop="userName">
            <el-input v-model="form.userName"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="createDialog = false">取 消</el-button>
          <el-button type="primary" @click="saveUser">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </el-drawer>
</template>

<script>
import { getUsersByGroupId, saveUser, removeUser } from '@/api/uc/user'

export default {
  props: ['dialogVisible'],
  data() {
    return {
      dialog: false,
      userGroupId: null,
      userList: null,
      createDialog: false,
      form: {
        type: 1,
        userCode: "",
        userName: "",
        mobile: "",
        email: ""
      },
      rules: {
        userCode: [{required: true, message: "用户编码不可为空", trigger: "change"},
          {pattern: /^[a-zA-Z0-9]+$/, message: '用户编码格式错误'}],
        userName: [{required: true, message: "用户名称不可为空", trigger: "change"}],
        mobile: [{required: true, message: "手机号不可为空", trigger: "change"},
          {pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/, message: '手机号格式错误'}],
        email: [{required: true, message: "邮箱不可为空", trigger: "change"},
          {pattern: /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/, message: '邮箱格式错误'}]
      }
    }
  },
  watch: {
    dialogVisible(val) {
      this.dialog = val.dialogVisible
      this.userGroupId = val.userGroupId
      this.getUsers()
    }
  },
  methods: {
    removeUser(row) {
      this.$confirm('是否确定删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let removeData = {
          userId: row.userId,
          userGroupId: this.userGroupId
        }
        removeUser(removeData).then(res => {
          this.$message.success('删除成功')
          this.getUsers()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })

    },
    closeDialog() {
      this.$refs.form.resetFields()
    },
    saveUser() {
      this.$refs.form.validate(vali => {
        console.log('vali', vali)
        console.log('this.form', this.form)
        if (vali) {
          this.form.userGroupId = this.userGroupId
          saveUser(this.form).then((res) => {
            this.$message.success('添加成功')
            this.createDialog = false
            this.getUsers()
          })
        }
      })
    },
    openCreateDialog() {
      this.createDialog = true
    },
    /**
     * 获取员工列表
     */
    getUsers() {
      getUsersByGroupId({ userGroupId: this.userGroupId }).then(res => {
        this.userList = res.data.data
      })
    }
  }
}
</script>
